﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace myking
{
    public partial class FrmHelp : Form
    {
        public FrmHelp()
        {
            InitializeComponent();
        }

        private void btncheck_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtinput.Text) || !string.IsNullOrEmpty(txtinput.Text))
                {
                    richTextBox1.Text = "";
                    mathvalue = new Dictionary<int, string>();
                    if (InputMatches(txtinput.Text, txtpattern.Text))
                    {
                        
                        foreach (var item in mathvalue)
                        {
                            richTextBox1.Text += "%" + item.Key + " = " + item.Value + "\r\n";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                richTextBox1.Text = ex.Message;
            }
        }

        private void btnnormal_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtinput.Text) || !string.IsNullOrEmpty(txtinput.Text))
            {
                richTextBox1.Text = "";
                //模拟匹配
                if (System.Text.RegularExpressions.Regex.IsMatch(txtinput.Text, txtpattern.Text))
                {
                    richTextBox1.Text = "匹配成功!";
                    return;
                }
                richTextBox1.Text = "匹配失败!";
            }
        }

        /// <summary>
        /// 游戏匹配变量
        /// </summary>
        public static Dictionary<int, string> mathvalue { get; set; }


        public bool InputMatches(string input, string pattern)
        {
            try
            {
                mathvalue = new Dictionary<int, string>();

                int index = 1;
                Boolean first = true;

                MatchCollection matches = Regex.Matches(input, pattern);
                foreach (Match match in matches)
                {
                    foreach (Group group in match.Groups)
                    {
                        if (!first)
                        {
                            Boolean find = false;
                            foreach (var item in mathvalue)
                            {
                                if (item.Key == index)
                                {
                                    find = true;
                                    break;
                                }
                            }
                            if (find)
                            {
                                mathvalue[index] = group.Value;

                            }
                            if (!find)
                            {
                                mathvalue.Add(index, group.Value);
                                index++;
                            }
                        }
                        if (first)
                        {
                            first = false;
                        }

                    }
                }

                if (matches.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }


    }
}



